package com.hqyj.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.SupplierFoodListMapper;
import com.hqyj.pojo.SupplierFoodList;
import com.hqyj.service.SupplierFoodListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class SupplierFoodListServiceImpl implements SupplierFoodListService {

    //注入mapper
    @Autowired(required = false)
    SupplierFoodListMapper supplierFoodListMapper;


    @Override
    public HashMap<String, Object> select(SupplierFoodList f) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<SupplierFoodList> page = new Page<>(f.getPage(),f.getRow());
        QueryWrapper<SupplierFoodList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(f.getFoodId()!=null,"food_id",f.getFoodId());
        queryWrapper.like(f.getFoodName()!=null&&!f.getFoodName().equals(""),"food_name",f.getFoodName());
        Page<SupplierFoodList> fList = supplierFoodListMapper.selectPage(page, queryWrapper);
        map.put("list",fList.getRecords());
        //总页数
        map.put("totalPage",fList.getPages());
        //总条数
        map.put("total",fList.getTotal());
        //当前页
        map.put("curPage",f.getPage());
        if(f.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",f.getPage()-1);

        }
        if(f.getPage()==fList.getPages()){
            //下一页
            map.put("nextPage",fList.getPages());

        }else{
            map.put("nextPage",f.getPage()+1);

        }
        //每页显示条数
        map.put("row",f.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> add(SupplierFoodList f) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = supplierFoodListMapper.insert(f);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(SupplierFoodList f) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //修改
        int num = supplierFoodListMapper.updateById(f);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer f) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num = supplierFoodListMapper.deleteById(f);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }
}

